package com.key.org.action;

import com.key.base.action.BaseAction;
import com.key.org.model.Person;
import com.key.org.service.IPersonService;
import com.key.util.XspMessage;

public class PersonAction extends BaseAction {

	IPersonService personService;
	private Person person;

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public IPersonService getPersonService() {
		if (personService == null)
			personService = (IPersonService) getBean("personServiceImp");
		return personService;
	}

	public void setPersonService(IPersonService personService) {
		this.personService = personService;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check
			if (person.getDepartment().getId().equals("")) {
				person.setDepartment(null);
			}
			if (person.getPost().getId().equals("")) {
				person.setPost(null);
			}
			getPersonService().add(person);
		} catch (Exception e) {
			return xspMessage
					.setMessage("保存失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// check
			if (person.getDepartment().getId().equals("")) {
				person.setDepartment(null);
			}
			if (person.getPost().getId().equals("")) {
				person.setPost(null);
			}
			getPersonService().update(person);
		} catch (Exception e) {
			return xspMessage
					.setMessage("更新失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}
}
